Page({
  data: {
    customerCount:0,
    cheferCount:0,
    app:null,
    db:null,
    isLoggedIn: false,
    userInfo: {},
    menus: [
      // { id: 'order', title: '全部订单', icon: '/images/icons/goods-active.png' },
      // { id: 'address', title: '收货地址', icon: '/images/icons/goods-active.png' },
      { id: 'service', title: '联系客服', icon: '/images/icons/goods-active.png' },
      { id: 'settings', title: '系统设置', icon: '/images/icons/goods-active.png' }
    ]
  },
  onShow() {
    this.checkLoginStatus();
  },

  checkLoginStatus() {
    const app = getApp();
    this.setData({
      app:app
    })
    console.log(app.globalData.env); // 输出: your-env-id
    // 获取默认环境的数据库引用
    this.setData({
      db:app.globalData.db
    })
    const userInfo = wx.getStorageSync('userInfo');
    this.setData({
      isLoggedIn: !!userInfo,
      userInfo: userInfo || {}
    });
    this.getCustomerCount()
    this.getChefCount()
  },
  getCustomerCount(){
    this.data.db.collection('goodsList').where({customerId:this.data.userInfo._openid})
    .get()
    .then(res => {
      if(res && res.data && res.data.length > 0){
        console.log('res',res.data)
        this.setData({
          customerCount:res.data.length
        })
      }else{
        this.setData({
          customerCount:0
        })
      }
    })
  },
  getChefCount(){
    this.data.db.collection('goodsList').where({cheferId:this.data.userInfo._openid})
    .get()
    .then(res => {
      if(res && res.data && res.data.length > 0){
        console.log('res',res.data)
        this.setData({
          cheferCount:res.data.length
        })
      }else{
        this.setData({
          cheferCount:0
        })
      }
    })
  
  },
  navigateToEdit() {
    wx.navigateTo({ url: '/pages/userinfo/index' });
  },

  handleMenuTap(e) {
    console.log('e',e)
    const { id } = e.currentTarget.dataset;
    const urlMap = {
      order: '/pages/order/index',
      address: '/pages/address/index',
      service: '/pages/service/index',
      settings: '/pages/settings/index'
    };
    wx.showToast({ title: '暂未开放', icon: 'none' })
    return;
    if (urlMap[id]) wx.navigateTo({ url: urlMap[id] });
  },

  showLogoutConfirm() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorageSync();
          this.setData({ isLoggedIn: false, userInfo: {} });
          wx.showToast({ title: '已退出', icon: 'success' });
          wx.reLaunch({
            url: '/pages/login/index'
          })
        }
      }
    });
  }
});